setup.py: Handle python binding instead of cmake
authorBoyuan Yang <byang@debian.org>
Tue, 13 Jan 2026 23:04:57 +0000 (18:04 -0500)
committerBoyuan Yang <byang@debian.org>
Wed, 14 Jan 2026 00:17:36 +0000 (19:17 -0500)
Gbp-Pq: Name 0009-setup.py-Handle-python-binding-instead-of-cmake.patch

setup.py

index f32590e802e5af7721f599c91dff906737bf364b..1d062e1d827db5d1078f949bba0653af88dc099b 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -8,6 +8,8 @@ import setuptools
 import setuptools.command.build_ext
 import wheel.bdist_wheel
 
+import pybind11
+
 _this_dir = os.path.dirname(os.path.abspath(__file__))
 _build_dir = os.path.join(_this_dir, 'build', 'python')
 
@@ -111,10 +113,13 @@ class OpenCCExtension(setuptools.Extension, object):
 
 class BuildExtCommand(setuptools.command.build_ext.build_ext, object):
     def build_extension(self, ext):
-        if self.inplace:
-            output_path = os.path.join(_this_dir, 'python', 'opencc', 'clib')
-        else:
-            output_path = os.path.abspath(os.path.join(self.build_lib, 'opencc', 'clib'))
+        project_root = os.path.abspath(_this_dir)
+        build_dir = os.environ.get('OPENCC_BUILD_DIR', 'obj-x86_64-linux-gnu')
+        lib_path = os.path.join(project_root, build_dir, 'src')
+        ext.include_dirs.append(pybind11.get_include())
+        ext.include_dirs.append('src')
+        ext.library_dirs = [lib_path]
+        ext.runtime_library_dirs = [lib_path]
         if isinstance(ext, OpenCCExtension):
             build_libopencc(output_path)
         else:
@@ -174,7 +179,14 @@ setuptools.setup(
 
     packages=packages,
     package_dir={'opencc': 'python/opencc'},
-    ext_modules=[OpenCCExtension('opencc.clib.opencc_clib', 'python')],
+    ext_modules=[
+        setuptools.Extension(
+            'opencc.clib.opencc_clib',
+            sources=['src/py_opencc.cpp'],
+            libraries=['opencc'],
+            language='c++'
+        )
+        ],
     cmdclass={
         'build_ext': BuildExtCommand,
         'bdist_wheel': BDistWheelCommand